function newDateFromString(str) {
	str = str.split('-'); 
	var date = new Date(); 
	date.setUTCFullYear(str[0], str[1] - 1, str[2]); 
	date.setUTCHours(0, 0, 0, 0); 
	return date; 
} 

var AppTools = {
	weekText : function(a, b) {
		$(a).on(
				"change",
				function() {

					var date = $(a).val();
					var day = newDateFromString(date);
					var today = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
							'Friday', 'Saturday');
					var week = today[day.getDay()];
					$(b).val(week);
				});
	},

	weekText2 : function(a, b1,b2) {
		$(a).on(
				"change",
				function() {

					var date = $(a).val();
					var day = newDateFromString(date);
					var today = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
							'Friday', 'Saturday');
					var week = today[day.getDay()];
					$(b1).val(week);
					$(b2).val(week);

				});
	},
	dataPickerSetDefault:function(){
		$.datepicker.setDefaults({
			dateFormat: "yy-mm-dd",
			showOtherMonths: true,
//			maxDate: new Date(),
//			minDate:"1950-01-01",
			selectOtherMonths: false,
			//isRTL:true,
			monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],  
			monthNamesShort: ['1','2','3','4','5','6','7','8','9','10','11','12'],  
	        dayNames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],  
	        dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],  
	        dayNamesMin: ['日','一','二','三','四','五','六'],  
	        weekHeader: '周',  
			changeMonth: true,
			changeYear: true,
			firstDay: 1,  
			 showMonthAfterYear: true,  
		        yearSuffix: '年' 
		});
	},
	chosenSetDefault:function(data){
		
		var d = {allow_single_deselect:true,search_contains: true};
		if(data!=null){
			d = data;
		}
		if(!ace.vars['touch']) {
			$('.chosen-select').chosen(d); 
			//resize the chosen on window resize

			$(window)
			.off('resize.chosen',function(){alert(2);})
			.on('resize.chosen', function() {
				$('.chosen-select').each(function() {
					 var $this = $(this);
					 //$this.next().css({'width': '100%'});
					 $this.next().css({'width': $this.parent().width()});
					 //$this.next().css({'width': '100%'});
					 
					 
				});
			}).trigger('resize.chosen');
			
			$(document).on('settings.ace.chosen', function(e, event_name, event_val) {
				if(event_name != 'sidebar_collapsed') return;
				$('.chosen-select').each(function() {
					 var $this = $(this);
					 $this.next().css({'width': $this.parent().width()});
					 //$this.next().css({'width': '100%'});
				});
			});
	

			
			
			$('#chosen-multiple-style .btn').on('click', function(e){
				var target = $(this).find('input[type=radio]');
				var which = parseInt(target.val());
				if(which == 2) $('#form-field-select-4').addClass('tag-input-style');
				 else $('#form-field-select-4').removeClass('tag-input-style');
			});

		}
	},
	bindValue : function(fromValue, toValue) {
		$(fromValue).on(
				"change",
				function() {
					var date = $(fromValue).val();
					$(toValue).val(date);
				});
	},
	daysFromTwoDate:function(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
	    var oDate1 = newDateFromString(sDate1);
	    var oDate2 = newDateFromString(sDate2);
	    var iDays  =  parseInt((oDate2  -  oDate1)  /  1000  /  60  /  60  /24);    //把相差的毫秒数转换为天数  //Math.abs
	    return  iDays;
	},

	yearsFromNow:function(targetDate){    //sDate1和sDate2是2006-12-18格式  
	    var oDate1 = newDateFromString(targetDate);
	    var oDate2 = new Date();
	    var iDays  =  parseInt((oDate2  -  oDate1)  /  1000  /  60  /  60  /24);    //把相差的毫秒数转换为天数  //Math.abs
	    return  iDays;
	},
	getSettingValueFromList:function(pname,list){
		var length = list.length;
		for(var i=0;i<length;i++){
			var settingObj = list[i];
			if(settingObj.name==pname){
				return settingObj.value;
			}
		}
		return "";
	},
	notNull:function(dataPost){
		if(dataPost==null){
			return "";
		}
		return dataPost;
	},
	addCopyListener:function(){
		 $(".jtablestyle").find("th").dblclick(function(){
			var cindex = $(this).index();
			var trs = $(this).parents(".jtablestyle").find("tbody > tr");
			var content = "";
			for(var i=0;i<trs.length;i++){
				var td = $(trs[i]).find("td")[cindex];
				var txt = $(td).html();
				content+=txt+"<br>";
			}
			if (window.clipboardData){
			    window.clipboardData.setData("Text", content);
			}else{
//				${_res.get("label.colDataCopy")}
				var diaDiv = '<div id="dialog" title=" &nbsp;&nbsp;" style="white-space: normal;word-wrap:break-all"><div style="height:320px;"><p id="contentid" style="border:1px solid #669FC7;padding-left:10px;">'+content+'</p></div></div>';
			    $( diaDiv ).dialog();
			}
		});
	}
};